package com.qf.web.service.impl;

import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.web.common.qo.MemberRequestParams;
import com.qf.web.common.vo.MemberVo;
import com.qf.web.entity.Member;
import com.qf.web.mapper.MemberMapper;
import com.qf.web.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    MemberMapper memberMapper;

    /**
     * @param requestParams
     * @return
     */
    @Override
    public MemberVo register(MemberRequestParams requestParams) throws Exception {
        Member member = memberMapper.selectMember(requestParams.getUsername(), requestParams.getPhone());
        if (ObjectUtils.isEmpty(member)) {
            member = new Member();
            // QO 转化 PO
            BeanUtils.copyProperties(requestParams, member);
            int count = memberMapper.insert(member);
            if (count > 0) {
//                PO 转化 VO
                MemberVo vo = new MemberVo();
                BeanUtils.copyProperties(member, vo);
                return vo;
            } else {
                throw new Exception("用户注册失败");
            }

        } else {
            throw new ServiceException(ResultCode.ACCOUNT_IS_EXISTENT);
        }

    }
}
